import { defineStore } from 'pinia';

export const useAppStore = defineStore('app-store', {
  state: () => ({
    theme: 'dark',
    followSystemTheme: true,
  }),
  actions: {
    setTheme(theme: string) {
      this.theme = theme;
    },
    setDarkTheme() {
      this.theme = 'dark';
    },
    changeNextTheme() {
      // auto / dark / light
      if (this.theme === 'auto') {
        this.theme = 'dark';
      } else if (this.theme === 'dark') {
        this.theme = 'light';
      } else {
        this.theme = 'auto';
      }
    },
    setFollowSystemTheme (follow: boolean) {
      this.followSystemTheme = follow;
    },
    getSystemTheme() {
      if (this.followSystemTheme) {
        const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
        this.theme = darkModeMediaQuery.matches ? 'dark' : 'light';
      }
    }
  },
});
